﻿package  
{
	import fl.controls.ScrollBarDirection;
	/**
	 * ...
	 * @author ml
	 */
	public class RollableStrip
	{
		public var items:Array;
		public var direction:String = ScrollBarDirection.HORIZONTAL;
		public var directionMod:int = 1;
		public var leftIndex:uint = 0;
		//public var rightIndex:uint = 0;
		public var showLimit:int = -1;
		public function get offsetAttribute():OffsetAttribute{
			if(direction == ScrollBarDirection.HORIZONTAL){
				return new OffsetAttribute({coord:'x',measure:'width'});
			}else if(direction == ScrollBarDirection.VERTICAL){
				return new OffsetAttribute({coord:'y',measure:'height'});
			}
			return new OffsetAttribute();
		}
		public function RollableStrip(_direction:String = ScrollBarDirection.HORIZONTAL, _directionMod:int = 1) 
		{
			direction = _direction;
			directionMod = _directionMod;
			items = new Array();
		}
		public function add(_new:*):void{
			if(_new != null){
				if(_new is Array){
					items.push(_new.shift());
					add(_new);
				}else if(_new is RollableStripItem){
					items.push(_new);
				}
			}
		}
		public function restack():void{
			var offset:Number = new Number();
			for(var i:int = 0; i < items.length; i++){
				var item:RollableStripItem = items[i];
				if(i < leftIndex){	// not shown, positioned to the left
					item.graphic[offsetAttribute.coord] = offset - item.positioning[offsetAttribute.measure];
				}else if(i < leftIndex + showLimit || showLimit == -1){	// shown, positioned between left and right
					item.show();
					item.graphic[offsetAttribute.coord] = offset + item.positioning[offsetAttribute.coord];
					offset += (item.positioning[offsetAttribute.measure] * directionMod);
				}else if(i >= leftIndex + showLimit){	// not shown, positioned to the right
					item.show(false);
					item.graphic[offsetAttribute.coord] = offset + item.positioning[offsetAttribute.coord];
				}
			}
		}
		public function show(_showLimit:int):void{
			showLimit = _showLimit;
			restack();
		}
		public function moveBy(delta:int):void{
			var _leftIndex:int = leftIndex + delta;
			moveTo(_leftIndex);
		}
		public function moveTo(_leftIndex:int):void{
			if(_leftIndex < 0){
				_leftIndex = 0;
			}else if(_leftIndex > items.length - showLimit){
				_leftIndex = items.length - showLimit;
			}
			leftIndex = _leftIndex;
			restack();
		}
	}
}